﻿using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using OpenIddict.EntityFrameworkCore.Models;
using WT.OpenIddict.Domain;
using WT.OpenIddict.Domain.Roles;
using WT.OpenIddict.Domain.User;

namespace WT.OpenIddict.EntityFrameWorkCore;

public class OpenIddictDbContext : IdentityDbContext<UserDetails, RoleDetails, Guid>
{
    public OpenIddictDbContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<OpenIddictEntityFrameworkCoreScope<Guid>> OpenIddictScopes { get; set; }
    public DbSet<OpenIddictEntityFrameworkCoreAuthorization<Guid>> OpenIddictAuthorization { get; set; }
    public DbSet<OpenIddictEntityFrameworkCoreApplication<Guid>> OpenIddictApplication { get; set; }
    public DbSet<OpenIddictEntityFrameworkCoreToken<Guid>> OpenIddictToken { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //配置字段的默认最长长度
        ConfigureMaxLength(modelBuilder);

        modelBuilder.Entity<OpenIddictEntityFrameworkCoreScope<Guid>>(b =>
        {
            b.ToTable(OpenIddictConst.DbTablePrefix + "OpenIddictScopes", OpenIddictConst.DbSchema);
        });
        modelBuilder.Entity<OpenIddictEntityFrameworkCoreAuthorization<Guid>>(b =>
        {
            b.ToTable(OpenIddictConst.DbTablePrefix + "OpenIddictAuthorization", OpenIddictConst.DbSchema);
        });
        modelBuilder.Entity<OpenIddictEntityFrameworkCoreApplication<Guid>>(b =>
        {
            b.ToTable(OpenIddictConst.DbTablePrefix + "OpenIddictApplication", OpenIddictConst.DbSchema);
        });
        modelBuilder.Entity<OpenIddictEntityFrameworkCoreToken<Guid>>(b =>
        {
            b.ToTable(OpenIddictConst.DbTablePrefix + "OpenIddictToken", OpenIddictConst.DbSchema);
        });
    }

    private void ConfigureMaxLength(ModelBuilder modelBuilder)
    {
        // 遍历所有实体类型
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            // 遍历实体的所有属性
        foreach (var property in entityType.GetProperties())
            // 如果属性类型是字符串
            if (property.ClrType == typeof(string))
            {
                // 检查是否已经有 [MaxLength] 注解
                var maxLengthAttribute = property.PropertyInfo?
                    .GetCustomAttributes(typeof(MaxLengthAttribute), false)
                    .OfType<MaxLengthAttribute>()
                    .FirstOrDefault();

                var stringSynataxAttribute = property.PropertyInfo?
                    .GetCustomAttributes(typeof(StringSyntaxAttribute), false)
                    .OfType<StringSyntaxAttribute>()
                    .FirstOrDefault();
                //if(maxLengthAttribute==null&& stringSynataxAttribute == null)
                //{
                //    // 设置默认的最大长度
                //    property.SetMaxLength(1024); // 这里设置为 256，可以根据需求调整
                //}
            }
    }
}